package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdChannel;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper,WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 获取频道列表
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 频道列表展示
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult getList(ChannelDto channelDto) {
        if (channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页
        Page<WmChannel> wmChannelPage=new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<WmChannel> lqw=new LambdaQueryWrapper<>();
        if (channelDto.getName()!=null){
            lqw.like(WmChannel::getName,channelDto.getName());
        }
        //排序
        lqw.orderByDesc(WmChannel::getCreatedTime);

        page(wmChannelPage,lqw);

        PageResponseResult pageResponseResult= new PageResponseResult
                ((int)wmChannelPage.getCurrent(),(int)wmChannelPage.getSize(),(int)wmChannelPage.getTotal());
        pageResponseResult.setData(wmChannelPage.getRecords());
        return pageResponseResult;
    }

    //删除频道
    @Override
    public ResponseResult removeChannelById(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = getById(id);
        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"频道启用状态,无法删除!");
        }
        removeById(id);
        return ResponseResult.okResult("删除成功!");
    }

    //修改频道
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //校验参数
        if (adChannel==null || adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<WmNews> wmNewsList = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, adChannel.getId()));
        if (wmNewsList.size()!=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"频道关联了文章,无法修改!");
        }
        WmChannel wmChannel=new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        updateById(wmChannel);
        return ResponseResult.okResult("");
    }

}
